﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace WindowsSignInPage
{
    public class LoginUser
    {
        public string LoginId { get; set; }
        public string Domain { get; set; }
        public string Pwd { get; set; }
        public string Comments { get; set; }
        public bool IsActive { get; set; }

        public LoginUser()
        { }

        public LoginUser(string loginId)
        {
            using (SqlConnection conn = CreateConnection())
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                SqlCommand cmd = new SqlCommand(GetSelectCommand(loginId), conn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    LoginId = ToString(dr["LoginId"]);
                    Domain = ToString(dr["Domain"]);
                    Pwd = ToString(dr["Pwd"]);
                    Comments = ToString(dr["Comments"]);
                    IsActive = ToString(dr["IsActive"]) == "1";
                }
                dr.Close();
                conn.Close();
            }

        }
        public void Save()
        {
            using (SqlConnection conn = CreateConnection())
            {
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                SqlCommand cmd = conn.CreateCommand();

                LoginUser user = new LoginUser(LoginId);
                if (string.IsNullOrEmpty(user.LoginId))
                {
                    cmd.CommandText = GetInsertCommand();
                }
                else
                {
                    cmd.CommandText = GetUpdateCommand();
                }
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }

        protected string ConnectionString = SiteConfig.Load().Connection;
        protected SqlConnection CreateConnection()
        {
            return new SqlConnection(ConnectionString);
        }
        protected string GetSelectCommand(string loginId)
        {
            return string.Format(@"Select LoginId,Domain,Pwd,Comments,IsActive from LoginUser
                                where LoginId='{0}' and IsActive=1", loginId);
        }
        protected string GetInsertCommand()
        {
            string insertSql = string.Format("insert into LoginUser values('{0}','{1}','{2}','{3}',1)", LoginId, Domain, Pwd, Comments);
            return insertSql;
        }
        protected string GetUpdateCommand()
        {
            string updateSql = string.Format("update LoginUser set LoginId='{0}',Domain='{1}',Pwd='{2}',Comments='{3}' where LoginId='{0}' ", LoginId, Domain, Pwd, Comments);
            return updateSql;
        }
        protected string ToString(object obj)
        {
            if (obj == null || obj == DBNull.Value)
                return string.Empty;
            else
                return obj.ToString();
        }

    }
}